package randomdemo;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Java生成指定位数不重复随机数
 */
public class DemoOne {


    private static final Object OBJECT = new Object();
    private static long bIndex = 0;

    /**
     * 可用多线程检测是否会产生相同随机数
     * @param length
     * @return
     */
    public static String createSerialNo(int length) {
        double max = Math.pow(10, length);
        String curSerial;
        synchronized (OBJECT) {
            if (++bIndex >= max){
                bIndex = 0;
            }
            curSerial = bIndex + "";
        }
        while (curSerial.length() < length) {
            curSerial = "0" + curSerial;
        }
        return curSerial;
    }

    @Test
    public void testCreateSerialNo() throws Exception {


        double max = Math.pow(10, 3);

        System.out.println("max:"+max);


        int [] num = new int[10];
        for (int i = 0; i < 10 ; i++) {
            num[i] = Math.round(10);
        }





        for (int i = 0; i < num.length; i++) {
            /*SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String now = sdf.format(new Date());
            System.out.println(now + createSerialNo(3));*/

            System.out.println(createSerialNo(3));

        }


       // System.out.println(Math.round(10));

    }
}
